package com.soft.collateral.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soft.collateral.constant.MessageConstant;
import com.soft.collateral.entity.WithdrawApply;
import com.soft.collateral.entity.WithdrawApproval;
import com.soft.collateral.result.Result;
import com.soft.collateral.service.WithdrawService;
import com.soft.collateral.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/withdraw")
@Api(tags = "后台系统-出库管理接口")
@RequiredArgsConstructor
public class WithdrawController {

    private final WithdrawService withdrawService;

    @ApiOperation("查询出库申请列表")
    @GetMapping("list")
    public Result list() {
        List<WithdrawApply> withdrawApplyList = withdrawService.listAll();
        if (!withdrawApplyList.isEmpty()) {
            return new Result(true, MessageConstant.QUERY_WITHDRAW_SUCCESS, withdrawApplyList);
        }
        return new Result(false, MessageConstant.QUERY_WITHDRAW_FAIL);
    }

    @ApiOperation("条件分页查询出库申请列表")
    @GetMapping("page")
    public Result page(PageQueryVO pageQueryVO) {
        Page<WithdrawApply> pageQuery = withdrawService.pageQuery(pageQueryVO);
        if (pageQuery != null) {
            return new Result(true, MessageConstant.QUERY_WITHDRAW_SUCCESS, pageQuery);
        }
        return new Result(false, MessageConstant.QUERY_WITHDRAW_FAIL);
    }

    @ApiOperation("新增出库申请")
    @PostMapping
    public Result create(@RequestBody WithdrawApply withdrawApply) {
        boolean success = withdrawService.saveWithdrawApply(withdrawApply);
        if (success) {
            return new Result(true, MessageConstant.ADD_WITHDRAW_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_WITHDRAW_FAIL);
    }

    @ApiOperation("通过ID查询出库申请信息")
    @GetMapping("{id}")
    public Result findById(@PathVariable Integer id) {
        WithdrawApply withdrawApply = withdrawService.getByIdWithDetails(id);
        if (withdrawApply != null) {
            return new Result(true, MessageConstant.QUERY_WITHDRAW_SUCCESS, withdrawApply);
        }
        return new Result(true, MessageConstant.QUERY_WITHDRAW_SUCCESS);
    }

    @ApiOperation("编辑出库申请")
    @PutMapping
    public Result update(@RequestBody WithdrawApply withdrawApply) {
        boolean success = withdrawService.updateWithdrawApply(withdrawApply);
        if (success) {
            return new Result(true, MessageConstant.EDIT_WITHDRAW_SUCCESS);
        }
        return new Result(false, MessageConstant.EDIT_WITHDRAW_FAIL);
    }

    @ApiOperation("删除出库申请")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable Integer id) {
        boolean success = withdrawService.removeByIdWithDetails(id);
        if (success) {
            return new Result(true, MessageConstant.DELETE_WITHDRAW_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_WITHDRAW_FAIL);
    }

    @ApiOperation("审核出库申请")
    @PostMapping("approve")
    public Result approve(@RequestBody WithdrawApproval approval) {
        boolean success = withdrawService.approveWithdrawApply(approval);
        if (success) {
            return new Result(true, MessageConstant.APPROVE_WITHDRAW_SUCCESS);
        }
        return new Result(false, MessageConstant.APPROVE_WITHDRAW_FAIL);
    }
}